package com.lonelyzhe.springboot.controller;

import com.lonelyzhe.springboot.bean.Book;
import com.lonelyzhe.springboot.service.impl.BookService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("book")
@ResponseBody
public class BookController extends BaseController {

    @Autowired
    BookService bookService;

    @RequestMapping("/all")
    public AjaxResult allList() {
        try {
            List<Book> books = bookService.selectAllBookList();
            return AjaxResult.success(books);
        } catch (Exception e) {
            return AjaxResult.error("发生错误");
        }
    }

    @RequestMapping("/list")
    public AjaxResult list(@RequestParam("category_id")int category_id) {
        try {
            List<Book> books = bookService.selectBookList(category_id);
            return AjaxResult.success(books);
        } catch (Exception e) {
//            e.printStackTrace();
            // 默认错误是500
            return AjaxResult.error("发生错误");
            // return error(AjaxResult.Type.WARN, "发生错误");
        }
    }

    @RequestMapping("/insert")
    public AjaxResult insertBook(@RequestBody Book book) {
        try {
            bookService.insertBook(book);
            return AjaxResult.success();
        } catch (Exception e) {
//            e.printStackTrace();
            // 默认错误是500
            return AjaxResult.error("发生错误");
            // return error(AjaxResult.Type.WARN, "发生错误");
        }
    }

    @RequestMapping("/update")
    public AjaxResult updateBook(@RequestBody Book book) {
        try {
            bookService.updateBook(book);
            return AjaxResult.success();
        } catch (Exception e) {
            return AjaxResult.error("发生错误");
        }
    }

    @RequestMapping("/delete")
    public AjaxResult deleteBook(@RequestParam("book_id")int book_id) {
        try {
            bookService.deleteBook(book_id);
            return AjaxResult.success();
        } catch (Exception e) {
            return AjaxResult.error("发生错误");
        }
    }

    //上传图片
    private static Logger logger = LoggerFactory.getLogger(BookController.class);
    @Value(value = "${image.path:img}")
    private String imagePath;

    @Value(value = "${image.host:http://localhost}")
    private String imageHost;

    @RequestMapping(value = "/upload/file", method = RequestMethod.POST)
    public AjaxResult saveFile(@RequestParam(value = "img") MultipartFile file) {
        StringBuffer fileName = new StringBuffer();
        fileName.append(UUID.randomUUID().toString().replaceAll("-", ""));
        String type = file.getContentType();
        if ("image/png".equals(type)) {
            fileName.append(".png");
        } else if ("image/jpeg".equals(type)) {
            fileName.append(".jpg");
        } else if ("image/gif".equals(type)) {
            fileName.append(".gif");
        } else {
            return AjaxResult.error("请选择.png.jpg格式的图片");
        }
        if (file.getSize() > 1024000L) {
            return AjaxResult.error("图片超过1Mb");
        }
        try {
            file.transferTo(new File("/home/hotel/img", fileName.toString()));
            String url = "image/"+fileName.toString();
            return AjaxResult.success(url);
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.error("保存失败");
        }
    }


}
